#ifndef SERVOAGENT_H
#define SERVOAGENT_H

#include <QObject>
#include <QMap>

#define SERVOAGENT ServoAgent::getInstance()

enum SERVONUM
{
    ALL, //所有轴
    FEED=1, //供粉缸
    MANUFACTURE=2, //成型缸
    POWDER = 3, //铺粉电机
    ERRNO = 404  //编号错误
};

enum MOTIONTYPE
{
    UP,
    DOWN
};

typedef struct
{
    int servoNO;
    int servoStepsPerMM;
    float servoSpeed ;
    float servoSpeedRev;
    int servoPowerRev;
    int servoPower;
    float servoAcceleration;
    float servoSpeedMax;
    float servoACCMax;
} ServoParas;

class ServoSyncThread;

class ServoAgent : public QObject
{
    Q_OBJECT

public:
    static ServoAgent& getInstance(){
        static ServoAgent instance;
        return instance;
    }

    QMap<SERVONUM,ServoParas> servoParasMap;

    bool initSoemEthercat(const std::string & ifname);

    bool disInitSoemEthercat();

    void loadAllServoConfig();

    bool servoEnable(SERVONUM num);

    bool servoDisEnable(SERVONUM num);   

    bool servoReset(SERVONUM num); 

    bool moveJog(SERVONUM num ,MOTIONTYPE motionType , double speed);

    bool moveJog(SERVONUM num , double relativeDistance);

    bool moveAbs(SERVONUM num , double pos , double speed);

    bool moveZero(SERVONUM num, double speed);

    bool moveStop(SERVONUM num);

    bool setZero(SERVONUM num);

    bool scraperMoveLeft(double speed);

    bool scraperMoveRight(double speed);

    bool scraperSpreading(double ratio = 1);

    bool scraperToZore(double ratio = 1);

    void setJogUnitDis(const int unitDis){
        jogUnitDis = unitDis;
    };

    int getJogUnitDis(){
        return jogUnitDis;
    };

    int getPowerPosMax(){
        return powerPosMax;
    };

    bool getErrstr(int,QString&);

private:
    ServoAgent();
    ~ServoAgent();

    ServoAgent(const ServoAgent&)=delete;
    ServoAgent& operator=(const ServoAgent&)=delete;

private:
    int jogUnitDis{1000};
    bool isInited{false};    
    double pcMulValue;

    int feedPosMax{0};
    int manPosMax{0};
    int powerPosMax{0};

};
#endif // SERVOAGENT_H


